<script>
var deviceCaller = TNM.Global.deviceCaller;
var globalActionConfig = IXW.Actions.configActions;
var deviceNames = TCM.Const.DeviceTypeNames;
var nvAlert = NV.Dialog.alert;

var currentLine = null;
/*构造tree基础节点*/
function getLeafInfo(arr){
	return IX.map(arr, function(item, idx){
		return {name: deviceNames[item], key: [item]};
	});
}
var TreeInfo = [
{name : "",  nodes : [
	{name : '数字摄像机', nodes : getLeafInfo([20, 21, 22])},
	getLeafInfo([50])[0],
	// getLeafInfo([51])[0],
	getLeafInfo([55])[0],
	{name : '网络和附属设备', nodes : getLeafInfo([64])}
]}];
var CurrentOne = {name: "", nodes: [
	// getLeafInfo([51])[0],
	getLeafInfo([55])[0],
	{name : '网络和附属设备', nodes : getLeafInfo([64])}
]};
//grid的显示列表
var DeviceGridCfgs = {
	"55-64" : {columns: "devName,devType,devUserName,devPassWord"},
	"20-21-22-50-55-64" : {columns: "devName,devType,devUserName,devPassWord"},
	"20-21-22" : {columns: "devName,devType,devIp,devUserName,devPassWord"},
	"20" : {columns: "devName,devType,devIp,devUserName,devPassWord"},
	"21" : {columns: "devName,devType,devIp,devUserName,devPassWord"},
	"22" : {columns: "devName,devType,devIp,devUserName,devPassWord"},
	"50" : {columns: "devName,devIp,devUserName,devPassWord"},
	"51" : {columns: "devName,devIp,devUserName,devPassWord"},
	"55" : {columns: "devName,devIp,devUserName,devPassWord"},
	"64" : {columns: "devName,devPath,devUserName,devPassWord"},
};

</script>
<tpl id="input"><div><input class="search" placeholder="设备选择"><a class="pic-search"></a></div></tpl>
<tpl id="info">
	<div id="Tree"><div id="treeName">{treeName}</div>{treeData}</div>
	<div id="Grid" class="{clz} nv-box"></div>
</tpl>

<script>
//Tree的点击函数
function devicTreeClick(params){
	var types = IX.map(params.keys.split(","),function(type){return Number(type);});
	var Index = types.join("-");
	createDeviceGrid(DeviceGridCfgs[Index], {
		types: types, 
		siteId: params.siteId - 0, 
		name: params.name + "列表", 
		el: params.el
	});
}
//初始化Tree的节点显示
function showTreeNode(){
	jQuery(".nv-tree>ul>li>ul>li").children("ul").hide();
	jQuery(".nv-tree>ul>li").children('ul').hide();
	jQuery(".nv-tree>ul>li>a span.count").hide();
}
function loginRowModel(rowData, colModels, actions, moreActions){
	var row = new IXW.Lib.GridModel.RowModelBase(rowData, colModels, actions, moreActions);
	return IX.inherit(row, {
		getTpldata : function(){
			var isLogin = row.get("type");
			var tpldata = row.getTpldata();
			tpldata.clz = IX.Array.isFound(isLogin, [20,21,22,50,51,64]) ? "" : "notLogin";
			return tpldata;
		}
	});
}
//创建显示表格
function createDeviceGrid(cfgs, treeCfg){
	var grid = null;
	var columns = $XP(cfgs, "columns").split(",");
	var types = treeCfg.types;
	var siteId = treeCfg.siteId;

	function loginItem(rowModel, rowEL){
		if(rowModel.get("loginIp")){
			var http="http://"+rowModel.get("loginIp")+(rowModel.get("loginPort")?":"+rowModel.get("loginPort"):"");
			window.open(http); 
		}else{	
			nvAlert("无可登录地址！");
		}
	}
	function editItem(rowModel, rowEl){
		TNM.DeviceDialog.editDevice(rowModel, function(){
			grid.refresh();
		}, siteId);
	}
	grid = new TCM.Lib.Grid($XP(cfgs, "container", 'Grid'),{
		title : "设备管理",
		rowModel :  loginRowModel,
		grid : {
			clz : "device-list-"+treeCfg.types[0],
			columns: columns,
			actions : [["edit", "编辑", editItem],["login", "登录", loginItem]],
			hasCheckbox : $XP(cfgs, "hasCheckbox") || true,
			usePagination : $XP(cfgs, "usePagination") || true,
			dataLoader : function(params, _cbFn){
				deviceCaller("getDevices4Type", IX.inherit(params, {
					types: treeCfg.types,
					siteId: treeCfg.siteId
				}), function(data){
					_cbFn(data);
					if ($X("Tree"))
						TNM.Lib.resizeTreeAndGrid($X("Tree"));
				});
			}
		},
		tools : {
			buttons: []
		},
		listen : {}
	});
	return grid;
}
//构造树形结构
function parseTree(treeInfo, countHT, siteId) {
	function getTreeNode(_treeNodes){
		return IX.map(_treeNodes,function(treeNode){
			return IX.inherit(treeNode, getAttr(treeNode));
		});
	}
	function getAttr(treeNode){
		if(treeNode.key)
			return {
				key : treeNode.key,
				count : countHT[treeNode.key[0]] ? countHT[treeNode.key[0]] : 0,
				siteId : siteId,
				nodes : []
			};
		var arr = [], count = 0;
		var nodes = treeNode.nodes === 0 ? [] : getTreeNode(treeNode.nodes);
		for (var i = 0; i < nodes.length; i++) {
			arr = arr.concat(nodes[i].key);
			count += nodes[i].count;
		}
		return {
			key : arr,
			count : count,
			siteId : siteId,
			nodes : nodes
		};
	}
	return getTreeNode(treeInfo).pop();
}
//获取设备列表
function getDevices(cbFn){
	deviceCaller("getAllDevices", {}, function(data){
		var arrCount = IX.map(data, function(site, idx){
			var countHT = {};
			IX.iterate(site.devices, function(device){
				countHT[device.type] = device.count;
			});
			var lineSite = TCM.LineInfo.getSites().get(site.siteId);
			var arr = [];
			if(lineSite.type == 1){
				arr = [IX.inherit(CurrentOne, {name: lineSite.name})];
			}else{
				TreeInfo[0].name = lineSite.name;
				arr = TreeInfo;
			}
			return parseTree(arr, countHT, site.siteId);
		});
		cbFn({nodes : arrCount});
	});
}
//调用获取设备列表，传入回调函数，用于数据显示
function showDeviceList(cbFn){
	getDevices(function(_cfg){
		var cfg = IX.inherit(_cfg, {
			htmlFn : function(node) {return node.name + '<span class="count">(' + node.count + ')</span>';},
			click : devicTreeClick
		});
		var tree = new TCM.Lib.Tree(cfg);
		$X('body').innerHTML = t_info.renderData("", {
			treeName : "线路设备列表",
			treeData : tree.getHTML(),
			clz : "device"
		});
		showTreeNode();
		jQuery(".nv-tree li:first a:first span.count").click();
	});
	cbFn();
}

IX.ns("TNM.Device");
TNM.Device.init = function(pageCfg, pageParams, cbFn){
	currentLine = TNM.Env.getSession().getLineInfo();
	showDeviceList(cbFn);
};
</script>
